﻿
namespace BigPixels
{
    using System.Collections.Generic;
    using System.IO;
    using System.Xml.Serialization;

    public static class XmlSerializer<T> where T : new()
    {
        public static T Load(string fileName)
        {
            T instance = default(T);

            if (File.Exists(fileName))
            {
                using (var fs = new FileStream(fileName, FileMode.Open))
                {
                    XmlSerializer xs = new XmlSerializer(typeof(T));
                    instance = (T)xs.Deserialize(fs);
                }
            }
            else
                throw new FileNotFoundException(string.Format("File [{0}] does not exist", fileName));

            return instance;
        }

        public static void Save(string fileName, T instance)
        {
            using (var writer = new StreamWriter(fileName))
            {
                XmlSerializer xs = new XmlSerializer(typeof(T));
                xs.Serialize(writer, instance);
            }
        }
    }
}
